package com.rc.evcharger.mq;

import com.alibaba.fastjson.JSON;
import com.rc.evcharger.biz.notify.NotifyStopChargeResult;
import com.rc.evcharger.common.config.MqCommandConfig;
import com.rc.evcharger.common.mq.MqProducer;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.redis.RedChargeDataService;
import com.rc.evcharger.service.order.OrderInfoService;
import com.rc.evcharger.vo.charge.StopChargeReq;
import com.rc.evcharger.service.CommandService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 3秒延迟发送停止指令
 * Create by Yang on 2019/11/15
 */
@Component("command8EDelayStopListener")
public class Command8EDelayStopListener implements ChannelAwareMessageListener {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final int MAX_COUNT = 2;

    @Autowired
    private CommandService commandService;

    @Autowired
    private MqProducer mqProducer;

    @Autowired
    private NotifyStopChargeResult notifyStopChargeResult;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private MqCommandConfig mqCommandConfig;

    @Autowired
    private RedChargeDataService redChargeDataService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        Integer notifyCount = 1;
        if (headers.size() > 0) {
            notifyCount = (int) headers.get("notifyCount");
        }

        logger.info("command8EDelayStopListener 收到了:{},channel:{}", msg, channel.getConnection());


        try {
            StopChargeReq stopChargeReq = JSON.parseObject(msg, StopChargeReq.class);

            //查询订单是否已结算
            OrderInfo order = orderInfoService.selectByOrderNum(stopChargeReq.getOrderNum());
            if ("2".equals(order.getStatus())) {
                return;
            }

            commandService.query_stop_charge(stopChargeReq);

            //message.getMessageProperties().setHeader("notifyCount", ++notifyCount);


            //if (notifyCount != null && notifyCount <= MAX_COUNT) {
            //    //发送停止指令
            //    commandService.query_stop_charge(stopChargeReq);
            //
            //    message.getMessageProperties().setHeader("notifyCount", ++notifyCount);
            //    //递归MQ消息
            //    mqProducer.send(mqCommandConfig.getCONTROLL_8E_DELAY_STOP(), msg, notifyCount, 3000);
            //} else {
            //
            //    //无法停止订单，则推送失败回调
            //    notifyStopChargeResult.notifyOrderStop(order.getOrderNum(), SocketConstant.CON1, SocketConstant.CON1, 0);
            //
            //    logger.info("command8EDelayStopListener 发起停止指令轮询完毕,推送停止失败回调:{}", msg);
            //    return;
            //}
        } catch (Exception e) {
            logger.error("command8EDelayStopListener 处理失败", e);
            return;
        }
    }
}
